home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / text / manipulation / code4.lha / Code.c next >
Encoding:
C/C++ Source or Header  |  1993-09-27  |  4.4 KB  |  200 lines

  1. /********************************************************************
  2.     Code.c v4.0 Copyright © 1993 by Jan Lindström 
  3.   
  4. ********************************************************************/
  5.  
  6. /* Includes */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. /* Defines */
  14.  
  15. #define BACK 13
  16.  
  17. #ifndef NULL
  18. #define NULL 0L
  19. #endif
  20.  
  21. #ifndef TRUE
  22. #define TRUE 1
  23. #define FALSE 0
  24. #endif
  25.  
  26. #ifndef BOOL
  27. #define BOOL int
  28. #endif
  29.  
  30. char vers[]="$VER: Code v4 (27-Sep-1993)";
  31.  
  32. char USAGE[]="CODE USAGE : Code -d|e[vb<kb>] <inputfile> <outputfile>\n\n"   \
  33.         "d       Decode Pc text -> Amiga text\ne       Encode Amiga text -> PC text\n" \
  34.         "v       Verbose mode\nb<kb>    Buffers in kilobytes\n";
  35.  
  36. /* Funktion prototypes */
  37.  
  38. void main(int,char **);
  39. void decode(FILE *,FILE *,BOOL);
  40. void encode(FILE *,FILE *,BOOL);
  41.  
  42. FILE *fp=NULL;
  43. FILE *fp2=NULL;
  44. BOOL verbose=FALSE;
  45. int mode=0;
  46. char *inbuff=NULL;
  47. char *outbuff=NULL;
  48. unsigned long buffsize=10;
  49.  
  50. void main(int argc,char **argv)
  51. {
  52.     register int j,max;
  53.  
  54.     printf("%c\x1b[;33mCode v4 \x1b[0m Copyright © 1993 by Jan Lindström.\n\n",12);
  55.     
  56.     if(argc != 4)
  57.     {
  58.         printf("%s",USAGE);
  59.         exit(20);
  60.     }
  61.  
  62.     if(argv[1][0]=='-')
  63.     {
  64.         max=strlen(argv[1]);
  65.  
  66.         for(j=1;j < max;j++)
  67.         {
  68.             if(argv[1][j]=='v')
  69.                 verbose=TRUE;
  70.             else if(argv[1][j]=='e')
  71.                 mode=1;
  72.             else if(argv[1][j]=='d')
  73.                 mode=2;
  74.             else if(argv[1][j]=='b')
  75.             {
  76.                 j++;
  77.                 buffsize=0;
  78.                 buffsize=atoi(&(argv[1][j]));
  79.  
  80.                 if(buffsize == 0)
  81.                 {
  82.                     mode=0;
  83.                     break;
  84.                 }
  85.  
  86.                 while(isdigit(argv[1][j]))
  87.                     j++;
  88.             }
  89.          }   
  90.     }
  91.             
  92.     if(mode == 0)
  93.     {
  94.         printf("%s",USAGE);
  95.         exit(20);
  96.     }
  97.         
  98.     buffsize*=1024;
  99.  
  100.     if(inbuff=(char *)malloc(buffsize))
  101.     {
  102.         if(outbuff=(char *)malloc(buffsize * 2))
  103.         {
  104.             if(fp=fopen(argv[2],"r"))
  105.             {
  106.                 if(fp2=fopen(argv[3],"w"))
  107.                 {
  108.                     if(mode == 2)
  109.                         decode(fp,fp2,verbose);
  110.                     else
  111.                         encode(fp,fp2,verbose);
  112.                 }
  113.                 else
  114.                     printf("Can't open output file %s.\n",argv[3]);
  115.             }
  116.             else
  117.                 printf("Can't open input file %s.\n",argv[2]);
  118.         }
  119.         else
  120.             printf("Not enought memory !\n");
  121.     }
  122.     else
  123.         printf("Not enought memory !\n");
  124.  
  125.     if(inbuff)  free(inbuff);
  126.     if(outbuff) free(outbuff);
  127.     if(fp)      fclose(fp);
  128.     if(fp2)     fclose(fp2);
  129.  
  130.     printf("Operation done.\n\n");
  131. }
  132.  
  133. void decode(FILE *fp,FILE *fp2,BOOL verbose)
  134. {
  135.     register unsigned long counter=0,i,j;
  136.  
  137.     while(i=fread((char *)inbuff,1,buffsize,fp))
  138.     {
  139.         counter+=i;
  140.  
  141.         for(j=0;j < i;j++)
  142.         {
  143.             switch(inbuff[j])
  144.             {
  145.                 case '\x84':    outbuff[j]='ä';break;        /* ä */
  146.                 case '\x8E':    outbuff[j]='Ä';break;        /* Ä */
  147.                 case '\x94':    outbuff[j]='ö';break;        /* ö */
  148.                 case '\x99':    outbuff[j]='Ö';break;        /* Ö */
  149.                 case '\x8F':    outbuff[j]='Å';break;       /* Å */
  150.                 case '\x86':    outbuff[j]='å';break;       /* å */
  151.                 case 13:        outbuff[j]=' ';break;        /* ctrl-m */
  152.                 case '\x1A':    outbuff[j]=' ';break;        /* ctrl-z */
  153.                 default:        outbuff[j]=inbuff[j];break;
  154.             }
  155.         }
  156.  
  157.         if(verbose)
  158.             printf("%cDecoded %ld bytes",BACK,counter);
  159.  
  160.         fwrite((char *)outbuff,1,j,fp2);
  161.     }
  162.  
  163.     printf("%cDecoded %ld bytes.\n\n",BACK,counter);
  164. }
  165.  
  166. void encode(FILE *fp,FILE *fp2,BOOL verbose)
  167. {
  168.     register unsigned long counter=0,i,j,m;
  169.  
  170.     while(i=fread((char *)inbuff,1,buffsize,fp))
  171.     {
  172.         for(j=0,m=0;j < i;j++,m++)
  173.         {
  174.             switch(inbuff[j])
  175.             {
  176.                 case '\xE4':    outbuff[m]=132;break;
  177.                 case '\xC4':    outbuff[m]=142;break;
  178.                 case '\xF6':    outbuff[m]=148;break;
  179.                 case '\xD6':    outbuff[m]=153;break;
  180.                 case '\xC5':    outbuff[m]=143;break;
  181.                 case '\xA9':    outbuff[m]=134;break;
  182.                 case  10:        outbuff[m]=13;outbuff[++m]=10;break;
  183.                 default:        outbuff[m]=inbuff[j];break;
  184.             }
  185.  
  186.         }
  187.  
  188.         counter+=m;
  189.  
  190.         if(verbose)
  191.             printf("%cEncoded %ld bytes.",BACK,counter);
  192.  
  193.         fwrite((char *)outbuff,1,m,fp2);
  194.         
  195.     }
  196.  
  197.     printf("%cEncoded %ld bytes.\n\n",BACK,counter);
  198. }
  199.  
  200.